package com.foreknow.view;

import com.foreknow.model.Admin;
import com.foreknow.service.AdminService;
import com.foreknow.service.BusinessService;
import com.foreknow.service.impl.AdminServiceImpl;
import com.foreknow.service.impl.BusinessServiceImpl;
import com.foreknow.view.MainWindow;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;

public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Login() {
        setTitle("login");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 603, 469);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D\uFF1A");
        lblNewLabel.setFont(new Font("宋体", Font.BOLD, 16));
        lblNewLabel.setBounds(97, 102, 72, 36);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801\uFF1A");
        lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 16));
        lblNewLabel_1.setBounds(110, 151, 72, 34);
        contentPane.add(lblNewLabel_1);

        textField = new JTextField();
        textField.setBounds(223, 103, 251, 34);
        contentPane.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(223, 152, 251, 34);
        contentPane.add(passwordField);

        JButton btnNewButton_1 = new JButton("\u6CE8\u518C");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Register register = new Register();
                register.setVisible(true);
                Login.this.dispose();
            }
        });
        btnNewButton_1.setBounds(254, 293, 86, 42);
        contentPane.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("\u53D6\u6D88");
        btnNewButton_2.setBounds(428, 293, 86, 42);
        contentPane.add(btnNewButton_2);

        JLabel lblNewLabel_2 = new JLabel("\u6B22\u8FCE\u4F7F\u7528");
        lblNewLabel_2.setFont(new Font("楷体", Font.BOLD | Font.ITALIC, 20));
        lblNewLabel_2.setBounds(252, 34, 100, 36);
        contentPane.add(lblNewLabel_2);

        JButton button = new JButton("\u767B\u5F55");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = textField.getText();
                String password = passwordField.getText();
                AdminService adminService = new AdminServiceImpl();
                Admin admin = adminService.login(username, password);
                if (admin != null) {
                    System.out.println("登录成功");
                    JOptionPane.showMessageDialog(null, "登录成功", "success", JOptionPane.INFORMATION_MESSAGE);
                    MainWindow mainwindow = new MainWindow();
                    //隐藏当前窗口
                    Login.this.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "登录用户名或密码错误", "fail", JOptionPane.WARNING_MESSAGE);
                    System.out.println("登录失败");
                }

            }
        });
        button.setBounds(83, 293, 86, 42);
        contentPane.add(button);
    }
}
